﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Data;
using XHD.Common;
using XHD.Server;
using System.IO;

namespace XHD.View
{
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            System.Timers.Timer time = new System.Timers.Timer(3000);
            time.AutoReset = true;
            time.Enabled = true;
            time.Start();
            time.Elapsed += new System.Timers.ElapsedEventHandler(this.getSMSReport);
        }

        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码
        }

        void getSMSReport(object sender, System.Timers.ElapsedEventArgs e)
        {
            CRM_SMS sms = new CRM_SMS();
            sms.autoStatus();
        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码
            Exception objErr = Server.GetLastError().GetBaseException();


            //判断是否已配置
            XHD.Server.install ins = new XHD.Server.install();
            int configed = ins.configed();

            if (configed == 0) return;

            BLL.Sys_log_Err ssle = new BLL.Sys_log_Err();
            Model.Sys_log_Err model = new Model.Sys_log_Err();

            model.Err_typeid = 2;
            model.Err_type = "V1.17 beta";
            model.Err_time = DateTime.Now;
            model.Err_url = PageValidate.InputText(Request.Url.ToString(), 500);
            model.Err_message = PageValidate.InputText(objErr.Message, int.MaxValue);
            model.Err_source = PageValidate.InputText(objErr.Source, 500);
            model.Err_trace = PageValidate.InputText(objErr.StackTrace, int.MaxValue);
            model.Err_ip = Request.UserHostAddress;

            var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];


            //检验Cookie是否已经存在 
            if (null == cookie)
            {
                model.Err_emp_id = -1;
                model.Err_emp_name = "未登录";
            }
            else
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                string CoockiesID = ticket.UserData;
                if (PageValidate.IsNumber(CoockiesID))
                {
                    BLL.hr_employee emp = new BLL.hr_employee();
                    int emp_id = int.Parse(CoockiesID);
                    DataSet dsemp = emp.GetList("id=" + emp_id);
                    string empname = dsemp.Tables[0].Rows[0]["name"].ToString();

                    model.Err_emp_id = emp_id;
                    model.Err_emp_name = empname;
                }
                else
                {
                    model.Err_emp_id = -1;
                    model.Err_emp_name = "异常登录";
                }
            }

            ssle.Add(model);

            //Server.ClearError();


        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时，才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer，则不会引发该事件。

        }
        protected void Application_BeginRequest(Object sender, EventArgs e)
        {

        }

        ////计算页面执行时间
        //protected void Application_BeginRequest(Object sender, EventArgs e)
        //{
        //    Application["StartTime"] = System.DateTime.Now;
        //}
        //protected void Application_EndRequest(Object sender, EventArgs e)
        //{
        //    System.DateTime startTime = (System.DateTime)Application["StartTime"];
        //    System.DateTime endTime = System.DateTime.Now;
        //    System.TimeSpan ts = endTime - startTime;
        //    Response.Write("页面执行时间:" + ts.Milliseconds + " 毫秒");
        //}

    }
}
